﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Component;
using System.IO;

public partial class BackEnd_IntroductionImgList : System.Web.UI.UserControl
{
    protected string onclick = "";
    public string nameImage = "";
    public string _Url = BackEndQuery._AdminQuery + ConstString.NewL;
    FileInfo[] alFile = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        DataBind(IntroductionImgDB.GetAll());
        this.onclick = @"OpenWindowUploadFile(""FileManagement/File.aspx?id=a&idimg=b"",""window"",""width = 630px, height =520px"");";
        grid.RowCancelingEdit += delegate {  Response.Redirect(Request.Url.ToString()); };
    }
    /// <summary>
    ///   public void ReGetAllImge(object sender, System.EventArgs e)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void ReGetAllImge(object sender, System.EventArgs e)
    {
        GetAllImg();
        DataBind(IntroductionImgDB.GetAll());
    }
    /// <summary>
    ///    public void GetAllImg()
    /// </summary>
    public void GetAllImg()
    {
        string path = Server.MapPath(ConstString._PathFileIntroductionImg);
        alFile = IntroductionImgDB.GetAllFileImg(path);
        int order = 1;
        foreach (FileInfo file in alFile)
        {
            string path1 = "FileUpload/SlideShowIntroduction/" + file.Name;
            if (IntroductionImgDB.TestExistFile(path1) == false)
            {
                IntroductionImg info = new IntroductionImg(KetNoiCSDL.session);
                Introduction intro = IntroductionDB.GetObjectByid(1,true);
                info.Path = path1;
                info.Intro = intro;
                info.Priority = order;
                order++;
                IntroductionImgDB.Save(info);
            }
        }
    }

    /// <summary>
    ///     void DataBind(object al)
    /// </summary>
    /// <param name="al"></param>
    void DataBind(object al)
    {
        if (al != null)
        {
            grid.DataKeyNames = new string[] { ColumIntroductionImg._Id };
            grid.DataSource = al;
            grid.DataBind();
        }
    }

    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;
        TextBox txtPriority = (TextBox)grid.Rows[e.RowIndex].FindControl("txtPriority");
        IntroductionImg intro = new IntroductionImg(KetNoiCSDL.session);
        intro = IntroductionImgDB.GetObjectByid(ConvertType.ToInt(id), true);
        intro.Priority = ConvertType.ToInt(txtPriority.Text.Trim());
        if (IntroductionImgDB.Save(intro) == 1)
        {
            this.lbMessage.Text = ConstString.capNhatTC;
        }
         Response.Redirect(Request.Url.ToString());
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            object id = grid.DataKeys[e.RowIndex].Value;
            IntroductionImg intro = IntroductionImgDB.GetObjectByid(ConvertType.ToInt(id), true);
            if (IntroductionImgDB.Delete(intro) == 1)
            {
                string path1 = HttpRuntime.AppDomainAppPath + intro.Path.Replace("../", "").Replace("/", "\\");
                FileInfo fi = new FileInfo(path1);
                if (fi.Exists == true)
                    fi.Delete();
                this.lbMessage.Text = ConstString.xoaTC;
            }
        }
        catch { }
        DataBind(Component.IntroductionImgDB.GetAll());
    }

    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        DataBind(Component.IntroductionImgDB.GetAll());
    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        DataBind(Component.IntroductionImgDB.GetAll());
    }

}
